package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.vo.RecordsVo;

public class RecordsDao{
	/*
	 * 根据姓名id和状态查找记录
	 */
	public RecordsVo getRecordsByNameId(int rId) throws SQLException{
		String sql = "SELECT o.*,c.cust_name, ss.status_name,e.emp_name, r.* FROM records r"
				+" INNER JOIN `order` o on r.record_name_id = o.order_id"
				+" INNER JOIN customer c on  o.order_cust_name_id = c.cust_id"
				+" INNER JOIN `status` ss on c.cust_status_id = ss.status_id"
				+" INNER JOIN employee e on r.record_payee_id = e.emp_id"
				+" where r.record_name_id = ? order by r.record_id desc limit 1";

		Map<String,Object> data = Jdbc.getInstace().executeQuery(sql, rId);
		RecordsVo record = null;
		if(data != null){
			record = builderRecords(data);
		}
		return record;
	}
	
	/*
	 * 获取订单详情
	 */
	public List<RecordsVo> getRecordsAllList(Integer rid) throws SQLException{
		String sql = "SELECT o.*,c.cust_name, ss.status_name,e.emp_name, r.* FROM records r"
				+" INNER JOIN `order` o on r.record_name_id = o.order_id"
				+" INNER JOIN customer c on  o.order_cust_name_id = c.cust_id"
				+" INNER JOIN `status` ss on c.cust_status_id = ss.status_id"
				+" INNER JOIN employee e on r.record_payee_id = e.emp_id"
				+" where r.record_name_id = ? order by r.record_id desc";
 
		List<Map<String,Object>> dataList = Jdbc.getInstace().executeQueryForList(sql,rid);
		List<RecordsVo> recordsList = null;
		if(dataList != null){
			recordsList = new ArrayList<>();
			for (Map<String,Object> data : dataList) {
				recordsList.add(builderRecords(data));
			}
		}
		return recordsList;
		}
	
	private RecordsVo builderRecords(Map<String, Object> data) {
		RecordsVo record = new RecordsVo();
			record.setRecordId(Integer.parseInt(data.get("record_id").toString()));
			record.setOrderCustName(data.get("cust_name").toString());
			record.setOrderCustNameId(Integer.parseInt(data.get("order_cust_name_id").toString()));
			record.setOrderId(Integer.parseInt(data.get("order_id").toString()));
			record.setRecordLoan(Integer.parseInt(data.get("record_loan").toString()));
			record.setRecordNowpaid(data.get("record_nowpaid").toString());
			record.setRecordPaid((String)data.get("record_paid"));
			record.setRecordPaidall(Integer.parseInt(data.get("record_paidall").toString()));
			record.setRecordPayee(data.get("emp_name").toString());
			record.setRecordPayeeId(Integer.parseInt(data.get("record_payee_id").toString()));
			record.setRecordTotalmoney(data.get("record_totalmoney").toString());
			record.setRecordUnpaid((String)data.get("record_unpaid"));
			record.setRecordPaidtime((Date) data.get("record_paidtime"));
			record.setCustomStatus(data.get("status_name").toString());
			return record;
	}

	public boolean insertRecords(RecordsVo record) throws SQLException {
		String sql = "INSERT INTO records (record_name_id,record_nowpaid,record_paid,record_unpaid,record_paidall,record_payee_id,record_loan) VALUES (?,?,?,?,?,?,?)";
		boolean result = Jdbc.getInstace().executeUpdate(sql,record.getOrderCustNameId(),record.getRecordNowpaid(),record.getRecordPaid(),record.getRecordUnpaid(),
				record.getRecordPaidall(),record.getRecordPayeeId(),record.getRecordLoan());
		return result;
	}
}
